<?php
require_once("../config.php");
require_once("../facebook.php");
require_once ("../utils/redirect.php");
session_start();
if(empty($_SESSION['facebook_id']))
	die("Not logged into facebook.");
$facebook_id = $_SESSION['facebook_id'];

// The following code will delete all notifications
   $app_id = APP_ID;
   $app_secret = APP_SECRET;

   // The following code will grant an access_token needed to make queries.
   $token_url = "https://graph.facebook.com/oauth/access_token?" .
     "client_id=" . $app_id .
     "&client_secret=" . $app_secret .
     "&grant_type=client_credentials";

   $access_token = file_get_contents($token_url);
   
   print_r($access_token); // print thr access token

   $signed_request = $_REQUEST["signed_request"]; 
   list($encoded_sig, $payload) = explode('.', $signed_request, 2);
   $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
  // $user_id = $data["user_id"];
     $user_id = $facebook_id;

   //Get all app requests for user
   $request_url ="https://graph.facebook.com/" .
     $user_id .
     "/apprequests?" .
     $access_token;
   $requests = file_get_contents($request_url);

   //Print all outstanding app requests
   echo '<pre>';
   print_r($requests);
   echo '</pre>';

   //Process and delete app requests
   $data = json_decode($requests);
   foreach($data->data as $item) {
    $id = $item->id;
    $delete_url = "https://graph.facebook.com/" .
    $id . "?" . $access_token;

    $delete_url = $delete_url . "&method=delete";
    $result = file_get_contents($delete_url);
    echo("Was the request deleted? " . $result);
   }
 ?>
 
 
 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
	<div id="profile_pics"></div>
	<head>
		<script type="text/javascript" src=<?php echo LIB_JQUERY; ?>></script>
		<script type="text/javascript">

	function getNotifications() {
		var query = FB.Data.query('SELECT notification_id FROM notification WHERE app_id = {0} AND recipient_id = {1}', <?=APP_ID;?>,  <? echo $facebook_id; ?>);
		//var queryPerm = FB.Data.query('SELCT uid FROM user WHERE app_id = {0}', <?=APP_ID;?>);
		query.wait(function(rows) {
			for (var i = 0; i < rows.length; i++) {
				//alert(rows[i].access_token);
				//alert (rows[i].notification_id);
				//rows[i].markRead;
				//alert(rows[i].sender_id);
				}
			});
		}
		</script>
				                          
  </head>
  <body>
   <div id="fb-root"></div>
		<script>
			// asynchronously loads Facebook Javascript SDK, then displays card/friends list.
			window.fbAsyncInit = function() {
				FB.init({
					appId  : <?=APP_ID;?>, // currently our FB app ID
					status : true, // check login status
					cookie : true, // enable cookies to allow the server to access the session
					xfbml  : true  // parse XFBML
				});
				getNotifications();
			};
			
			(function() {
				var e = document.createElement('script');
				e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
				e.async = true;
				document.getElementById('fb-root').appendChild(e);
			}());
			
		</script>
  </body>
   
</html>
